package com.lancoo.znbkxx.teacher.widget

import android.animation.TimeInterpolator
import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import androidx.core.content.ContextCompat
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.utils.DensityUtils.dp2px
//import me.jessyan.autosize.utils.AutoSizeUtils.dp2px
import kotlin.math.abs
import kotlin.math.min
import kotlin.math.roundToInt

/**
 * @author : Admin
 * @date   : 2019/7/19 17:37
 * @desc   :
 */
class CirclePercentBar @JvmOverloads constructor(private val mContext: Context,
    attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(mContext, attrs, defStyleAttr) {

    private val mArcColor: Int
    private val mArcWidth: Int
    private val mCenterTextColor: Int
    private val mCenterTextSize: Int
    private val mCircleRadius: Int
    private var arcPaint: Paint? = null
    private var arcCirclePaint: Paint? = null
    private var centerTextPaint: Paint? = null
    private var arcRectF: RectF? = null
    private var textBoundRect: Rect? = null
    private var mCurData = 0f
    private var mSuffix = "%"
    private val arcStartColor: Int
    private val arcBgColor: Int
    private val arcEndColor: Int
    private var startCirclePaint: Paint? = null
    private lateinit var valueAnimator: ValueAnimator

    init {
        val typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.CirclePercentBar,
            defStyleAttr, 0)
        mArcColor = typedArray.getColor(R.styleable.CirclePercentBar_arcColor, 0xff0000)
        mArcWidth = typedArray.getDimensionPixelSize(R.styleable.CirclePercentBar_arcWidth,
            dp2px(mContext, 20f))
        mCenterTextColor = typedArray.getColor(R.styleable.CirclePercentBar_centerTextColor,
            0x0000ff)
        mCenterTextSize = typedArray.getDimensionPixelSize(
            R.styleable.CirclePercentBar_centerTextSize, dp2px(mContext, 20f))
        mCircleRadius = typedArray.getDimensionPixelSize(R.styleable.CirclePercentBar_circleRadius,
            dp2px(mContext, 100f))
        arcStartColor = typedArray.getColor(R.styleable.CirclePercentBar_arcStartColor,
            ContextCompat.getColor(mContext, R.color.colorStart))
        arcEndColor = typedArray.getColor(R.styleable.CirclePercentBar_arcEndColor,
            ContextCompat.getColor(mContext, R.color.colorEnd))
        arcBgColor = typedArray.getColor(R.styleable.CirclePercentBar_arcBgColor,
            ContextCompat.getColor(mContext, R.color.colorCirclebg))
        typedArray.recycle()

        initPaint()

    }

    private fun initPaint() {

        startCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG)
        startCirclePaint!!.style = Paint.Style.FILL
        //startCirclePaint.setStrokeWidth(mArcWidth);
        startCirclePaint!!.color = arcStartColor

        arcCirclePaint = Paint(Paint.ANTI_ALIAS_FLAG)
        arcCirclePaint!!.style = Paint.Style.STROKE
        arcCirclePaint!!.strokeWidth = mArcWidth.toFloat()
        arcCirclePaint!!.color = arcBgColor
        arcCirclePaint!!.strokeCap = Paint.Cap.ROUND

        arcPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        arcPaint!!.style = Paint.Style.STROKE
        arcPaint!!.strokeWidth = mArcWidth.toFloat()
        arcPaint!!.color = mArcColor
        arcPaint!!.strokeCap = Paint.Cap.ROUND

        centerTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        centerTextPaint!!.style = Paint.Style.STROKE
        centerTextPaint!!.color = mCenterTextColor
        centerTextPaint!!.textSize = mCenterTextSize.toFloat()

        //圓弧的外接矩形
        arcRectF = RectF()

        //文字的边界矩形
        textBoundRect = Rect()

    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(measureDimension(widthMeasureSpec),
            measureDimension(heightMeasureSpec))
    }

    private fun measureDimension(measureSpec: Int): Int {
        var result: Int
        val specMode = MeasureSpec.getMode(measureSpec)
        val specSize = MeasureSpec.getSize(measureSpec)
        if (specMode == MeasureSpec.EXACTLY) {
            result = specSize
        } else {
            result = mCircleRadius * 2
            if (specMode == MeasureSpec.AT_MOST) {
                result = min(result, specSize)
            }
        }
        return result
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {

        canvas.rotate(-90f, (width / 2).toFloat(), (height / 2).toFloat())

        arcRectF!!.set((width / 2 - mCircleRadius + mArcWidth / 2).toFloat(),
            (height / 2 - mCircleRadius + mArcWidth / 2).toFloat(),
            (width / 2 + mCircleRadius - mArcWidth / 2).toFloat(),
            (height / 2 + mCircleRadius - mArcWidth / 2).toFloat())


        canvas.drawArc(arcRectF!!, 0f, 360f, false, arcCirclePaint!!)

        arcPaint!!.shader = SweepGradient((width / 2).toFloat(), (height / 2).toFloat(),
            arcStartColor, arcEndColor)
        canvas.drawArc(arcRectF!!, 0f, 360 * mCurData / 100, false, arcPaint!!)

        canvas.rotate(90f, (width / 2).toFloat(), (height / 2).toFloat())
        canvas.drawCircle((width / 2).toFloat(),
            (height / 2 - mCircleRadius + mArcWidth / 2).toFloat(), (mArcWidth / 2).toFloat(),
            startCirclePaint!!)

        val data = mCurData.toString() + mSuffix
        centerTextPaint!!.getTextBounds(data, 0, data.length, textBoundRect)
        canvas.drawText(data, (width / 2 - textBoundRect!!.width() / 2).toFloat(),
            (height / 2 + textBoundRect!!.height() / 2).toFloat(), centerTextPaint!!)

    }

    fun setPercentData(data: Float, suffix: String, interpolator: TimeInterpolator, type: Int) {
        valueAnimator = ValueAnimator.ofFloat(mCurData, data)
        if (type == 1) {
            valueAnimator.duration = 60000
        } else if (data == 100f) {
            valueAnimator.duration = 500
        } else {
            valueAnimator.duration = (abs(mCurData - data) * 30).toLong()
        }
        valueAnimator.addUpdateListener {
            val value = it.animatedValue as Float
            mCurData = (value * 10).roundToInt().toFloat() / 10
            mSuffix = suffix
            invalidate()
        }
        valueAnimator.interpolator = interpolator
        valueAnimator.start()
    }

    fun end() {
        mCurData = 0f
        valueAnimator.removeAllUpdateListeners()
        valueAnimator.end()
    }
}